package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.LinkedEntity;
import com.sgb.kjwl.model.entity.transport.MotorcadListEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.swgk.core.util.UserOperating;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.GoodsManageEvent;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * @author: 杨畔
 * @date: 2020/12/28 11:10
 * @Description: 货源管理model层
 */
public class LinkItemModel extends AndroidViewModel {
    public MutableLiveData<ListResponseV1<List<MotorcadListEntity>>> ldMotorcad = new MutableLiveData<>();
    public MutableLiveData<LinkedEntity> ldLinked = new MutableLiveData<>();

    public LinkItemModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取货源列表
     */
    public void getGoodsManagerList(Context context, int page, Integer state,int type) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        Map<String, Object> map = new HashMap<>();
        if (UserOperating.getInstance().isCompany()) {
            map.put("companyName", UserOperating.getInstance().getTeamOrCompanyName());
        }
        map.put("contacts",UserOperating.getInstance().getUserRealName());
        map.put("contactsNumber",UserOperating.getInstance().getPhone());
       // map.put("motorcadeName","");
        map.put("page", page);
        map.put("limit", 10);
        map.put("type",type);
        //全部信息传不传state
        if (state!=null&&state >= 0) {
            map.put("linkState",state);
        }
        NewAPIManager.getInstance().getLogisticsAPI().getMotorcadList(map).enqueue(new ICallBack<BaseEntity<ListResponseV1<List<MotorcadListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponseV1<List<MotorcadListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (!data.isSuccess()) {
                    ldMotorcad.postValue(new ListResponseV1<>());
                    return;
                }
                ldMotorcad.postValue(data.getData());
                if (data.getData().haveMoreData()) {
                  // mPage = data.getData().nextPage();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponseV1<List<MotorcadListEntity>>>> call, Throwable t) {
                super.onFailure(call, t);
                ldMotorcad.postValue(new ListResponseV1<>(false));
            }
        });
    }

    /**
     * 删除
     */
    public void deleteNo(Context context, String no) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().deleteProDriver(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if ("200".equals(data.getCode())) {
                    EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_AFFILIATED_SUCCESS));
                } else {
                    MToast.showToast(getApplication(), "删除失败");
                }
            }
        });
    }
    /**
     * 解除挂靠
     */
    public void relation(Context context, String no) {
        DialogHelper.showProgressDialog(context, "请稍等...");
        NewAPIManager.getInstance().getLogisticsAPI().unbindProDriver(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                DialogHelper.dismissProgressDialog();
                if ("200".equals(data.getCode())) {
                    EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_AFFILIATED_SUCCESS));
                } else {
                    MToast.showToast(getApplication(), "解除挂靠失败");
                }
            }
        });
    }
    /**
     * 获取挂靠
     */
    public void getLinkItemList(Context context, int page, Integer state,int type) {
        DialogHelper.showProgressDialog(context, "加载中", 0, false);
        Map<String, Object> map = new HashMap<>();
        if (UserOperating.getInstance().isCompany()) {
            map.put("companyName", UserOperating.getInstance().getTeamOrCompanyName());
        }
        map.put("contacts",UserOperating.getInstance().getUserRealName());
        map.put("contactsNumber",UserOperating.getInstance().getPhone());
        // map.put("motorcadeName","");
        map.put("page", page);
        map.put("limit", 10);
        map.put("type",type);
        //全部信息传不传state
        if (state!=null&&state >= 0) {
            map.put("linkState",state);
        }
        NewAPIManager.getInstance().getLogisticsAPI().getPendantList(map).enqueue(new ICallBack<BaseEntity<LinkedEntity>>() {
            @Override
            public void onSuccess(BaseEntity<LinkedEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (!data.isSuccess()) {
                    ldLinked.postValue(new LinkedEntity());
                    return;
                }
                ldLinked.postValue(data.getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<LinkedEntity>> call, Throwable t) {
                super.onFailure(call, t);
                ldLinked.postValue(new LinkedEntity());
            }
        });
    }

}
